package com.inga.utils.httpclient;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * 处理传过来的http的get请求和post请求
 *
 * Created by abing on 2015/11/25.
 */
public class CommonHttpRequest {

    private final static HttpClient httpClient = new DefaultHttpClient();

    /**
     *处理传过来的get请求
     *
     * @param url
     * @param cookieData
     * @return  response
     * @throws ClientProtocolException
     * @throws IOException
     */
    public HttpResponse getHttpRequest(String url,Map<String, String> cookieData) throws ClientProtocolException, IOException{

        HttpGet get = new HttpGet(url);
        get.setHeader("User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0; BOIE9;ZHCN)");

        if (cookieData != null) {
            boolean first = true;
            StringBuilder cookie = new StringBuilder();
            for (Map.Entry<String, String> me : cookieData.entrySet()) {
                if (first) {
                    first = false;
                } else {
                    cookie.append(";");
                }
                cookie.append(me.getKey() + "=" + me.getValue());
            }
            get.setHeader("Cookie", cookie.toString());
        }

        HttpResponse response = httpClient.execute(get);

        return response;
    }

    /**
     * 处理传过来的post请求
     *
     * @param url
     * @param cookieData
     * @param para
     * @return response
     * @throws ClientProtocolException
     * @throws IOException
     */
    public HttpResponse postHttpRequest(String url,Map<String, String> cookieData,List<NameValuePair> para) throws ClientProtocolException, IOException{

        HttpPost post = new HttpPost(url);
        post.setHeader("User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0; BOIE9;ZHCN)");
        if (cookieData != null) {
            boolean first = true;
            StringBuilder cookie = new StringBuilder();
            for (Map.Entry<String, String> me : cookieData.entrySet()) {
                if (first) {
                    first = false;
                } else {
                    cookie.append(";");
                }
                cookie.append(me.getKey() + "=" + me.getValue());
            }
            post.setHeader("Cookie", cookie.toString());
        }
        if (para != null) {
            UrlEncodedFormEntity uef = new UrlEncodedFormEntity(para, "UTF-8");
            post.setEntity(uef);
        }

        HttpResponse response = httpClient.execute(post);

        return response;
    }


    /**
     * 测试demo
     *
     * @param args
     */
    public static void main(String[] args){
        CommonHttpRequest http = new CommonHttpRequest();
        Map<String , String> cookie = new HashMap<String , String>();

        try {
            HttpResponse res = http.getHttpRequest("http://my.oschina.net/u/1023341/blog/526601?fromerr=LkDT6J07",null);
            HttpEntity entity = res.getEntity();
            String html = EntityUtils.toString(entity).trim();

            System.out.println(html);


        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}
